#!/bin/bash
ISO_DIR=/root/custom_os
ISO_VERSION=r4
ISO_NAME=CentOS-6.5_64_${ISO_VERSION}.iso

alias cp='cp'

if [ $# -ge 1 ]; then
    ISO_VERSION=$1
fi

if [ $# -ge 2 ]; then
    ISO_NAME=$2
else
    ISO_NAME=CentOS-6.5_64_${ISO_VERSION}.iso
fi

echo "ver: ${ISO_VERSION}; iso: ${ISO_NAME}"

#生成 x86_64-comps.xml
# sh mk_comps.sh $ISO_VERSION
cd  ${ISO_DIR}/iso
declare -x discinfo=$(head -1 .discinfo)
rm ${ISO_DIR}/iso/repodata/* -f

#createrepo   -g   ${ISO_DIR}/iso/repodata/x86_64-comps.xml  ${ISO_DIR}/iso
#createrepo -u "media://$discinfo" -g ${ISO_DIR}/iso/repodata/*-x86_64-comps.xml ${ISO_DIR}/iso
if [ "$ISO_VERSION" == "r2"  ]; then
    echo "use x86_64-comps_r2.xml"
    createrepo -u "media://$discinfo" -g ${ISO_DIR}/template/repodata/x86_64-comps_r2.xml ${ISO_DIR}/iso
elif [ "$ISO_VERSION" == "debug" ]; then
    echo "use x86_64-comps_debug.xml"
    createrepo -u "media://$discinfo" -g ${ISO_DIR}/template/repodata/x86_64-comps_debug.xml ${ISO_DIR}/iso
elif [ "$ISO_VERSION" == "r3" ]; then
    echo "use x86_64-comps_r3.xml"
    createrepo -u "media://$discinfo" -g ${ISO_DIR}/template/repodata/x86_64-comps_r3.xml ${ISO_DIR}/iso
else
    echo "use x86_64-comps_r4.xml"
    createrepo -u "media://$discinfo" -g ${ISO_DIR}/template/repodata/x86_64-comps_r4.xml ${ISO_DIR}/iso
fi


# 复制配置文件
cd ${ISO_DIR}

cat config/isolinux/ks_cdrom.inc     config/isolinux/ks_comm_1hard_vm.inc config/isolinux/ks_comm_2disk_vm.inc config/isolinux/ks_comm_3package_vm.inc config/isolinux/ks_comm_4post_vm.inc > iso/isolinux/ks_cdrom_vm.cfg
cat config/isolinux/ks_cdrom.inc     config/isolinux/ks_comm_1hard.inc config/isolinux/ks_comm_2disk.inc config/isolinux/ks_comm_3package.inc config/isolinux/ks_comm_4post.inc > iso/isolinux/ks_cdrom.cfg
cat config/isolinux/ks_usb_disk1.inc config/isolinux/ks_comm_1hard.inc config/isolinux/ks_comm_2disk.inc config/isolinux/ks_comm_3package.inc config/isolinux/ks_comm_4post.inc > iso/isolinux/ks_usb_disk1.cfg
cat config/isolinux/ks_usb_disk2.inc config/isolinux/ks_comm_1hard.inc config/isolinux/ks_comm_2disk.inc config/isolinux/ks_comm_3package.inc config/isolinux/ks_comm_4post.inc > iso/isolinux/ks_usb_disk2.cfg
cat config/isolinux/ks_usb_disk3.inc config/isolinux/ks_comm_1hard.inc config/isolinux/ks_comm_2disk.inc config/isolinux/ks_comm_3package.inc config/isolinux/ks_comm_4post.inc > iso/isolinux/ks_usb_disk3.cfg

rm -f ${ISO_DIR}/iso/isolinux/isolinux.cfg

# r2版本 iso
if [ "$ISO_VERSION" == "r2" ]; then
    echo "iso_r2"
    cp -f ${ISO_DIR}/config/isolinux/isolinux.cfg  ${ISO_DIR}/iso/isolinux/isolinux.cfg
# r3版本 iso
elif [ "$ISO_VERSION" == "r3" ]; then
    echo "iso_r3"
    cp -f ${ISO_DIR}/config/isolinux/isolinux.cfg_org  ${ISO_DIR}/iso/isolinux/isolinux.cfg

    echo "PrepareMySQL 5.1 package..."
    rm -f  ${ISO_DIR}/iso/Packages/mysql*
    rm -f  ${ISO_DIR}/iso/Packages/libnids-*
    cp ${ISO_DIR}/template/R3_packages/* ${ISO_DIR}/iso/Packages/.
# r4版本 iso
elif [ "$ISO_VERSION" == "r4" ]; then
    echo "iso_r4"
    cp -f ${ISO_DIR}/config/isolinux/isolinux.cfg_org  ${ISO_DIR}/iso/isolinux/isolinux.cfg

    echo "PrepareMySQL 5.7 package..."
    rm -f  ${ISO_DIR}/iso/Packages/mysql*
    rm -f  ${ISO_DIR}/iso/Packages/libnids-*
    cp ${ISO_DIR}/template/R4_packages/* ${ISO_DIR}/iso/Packages/.
fi

# 修改首界面上的文字说明,增加日期
sed -i "s/menu title .*$/menu title CentOS 6.5 64bit apm $ISO_VERSION $(date "+%Y-%m-%d %H:%M:%S")/g" ${ISO_DIR}/iso/isolinux/isolinux.cfg

#生成 centos
cd iso
rm -f ../${ISO_NAME}; mkisofs -o ../${ISO_NAME} -input-charset utf-8 -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -R -J -v -T -joliet-long  ${ISO_DIR}/iso 
cd ..
/usr/bin/implantisomd5 ${ISO_NAME} 


